package com.begye.smartloan.apptest.path

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View

class CustomBorderView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {

    private val borderPaint = Paint().apply {
        color = 0xFFFFA500.toInt()  // 橙色的边框
        style = Paint.Style.STROKE   // 仅绘制边框
        strokeWidth = 10f            // 边框宽度
        isAntiAlias = true           // 抗锯齿
    }

    private val fillPaint = Paint().apply {
        color = 0xFFFDF5E6.toInt()  // 米白色的填充色
        style = Paint.Style.FILL     // 填充
        isAntiAlias = true           // 抗锯齿
    }

    private val path = Path()

    @SuppressLint("DrawAllocation")
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        val width = width.toFloat()
        val height = height.toFloat()
        val cornerRadius = 50f  // 圆角半径
        val triangleHeight = 50f  // 顶部小三角形的高度

        // 清空之前的路径
        path.reset()

        // 设置路径：开始绘制形状
        path.moveTo(cornerRadius, triangleHeight)  // 从左上圆角下的点开始
        path.lineTo(width - cornerRadius, triangleHeight)  // 到右上角前
        path.arcTo(RectF(width - cornerRadius * 2, triangleHeight, width, triangleHeight + cornerRadius * 2), -90f, 90f)  // 右上圆角

        path.lineTo(width, height - cornerRadius)  // 右下角前
        path.arcTo(RectF(width - cornerRadius * 2, height - cornerRadius * 2, width, height), 0f, 90f)  // 右下圆角

        path.lineTo(cornerRadius, height)  // 到左下角前
        path.arcTo(RectF(0f, height - cornerRadius * 2, cornerRadius * 2, height), 90f, 90f)  // 左下圆角

        path.lineTo(0f, triangleHeight + cornerRadius)  // 到左上角前
        path.arcTo(RectF(0f, triangleHeight, cornerRadius * 2, triangleHeight + cornerRadius * 2), 180f, 90f)  // 左上圆角

        // 绘制顶部的三角形
        path.lineTo((width / 2) - triangleHeight, triangleHeight)
        path.lineTo(width / 2, 0f)
        path.lineTo((width / 2) + triangleHeight, triangleHeight)

        path.close()

        // 绘制填充
        canvas.drawPath(path, fillPaint)
        // 绘制边框
        canvas.drawPath(path, borderPaint)
    }
}
